package cn.mutsuki.micrometer.core;

import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConditionalOnProperty(name = "micrometer.customizer.enable" , havingValue = "enabled")
//@AutoConfigureAfter({MtkMeterRegistryAutoConfiguration.MtkMeterPropertiesAutoConfiguration.class})
public class MtkMeterRegistryAutoConfiguration {

    @Bean
    @ConfigurationProperties("micrometer.customizer.common")
    @ConditionalOnMissingBean
    public MtkMeterRegistryCommonProperties mtkMeterRegistryCommonProperties(){
        MtkMeterRegistryCommonProperties mtkMeterRegistryCommonProperties = new MtkMeterRegistryCommonProperties();
        //mtkMeterRegistryCommonProperties.addMeterFilter(MeterFilter.acceptNameStartsWith("jvm")).addMeterFilter(MeterFilter.deny());
        return mtkMeterRegistryCommonProperties;
    }

//    @Configuration
//    @ConditionalOnProperty(name = "micrometer.customizer.meter.enable" , havingValue = "enabled")
//    static class MtkMeterPropertiesAutoConfiguration{
//
//        @Bean
//        @ConfigurationProperties("micrometer.customizer.meter.counter")
//        public MeterProperties CounterMeterProperties(){
//            return new MeterProperties(MeterProperties.MeterType.COUNTER);
//        }
//
//        @Bean
//        @ConfigurationProperties("micrometer.customizer.meter.gauge")
//        public MtkMeterConfiguration.MeterProperties GaugeMeterProperties(){
//            return new MtkMeterConfiguration.MeterProperties(MtkMeterConfiguration.MeterType.GAUGE);
//        }
//    }

//    @Bean
//    @ConditionalOnMissingBean
//    @Autowired
//    public MtkMeterConfiguration mtkMeterConfiguration(ApplicationContext applicationContext){
//        MtkMeterConfiguration mtkMeterConfiguration = new MtkMeterConfiguration();
//        Map<String,MeterProperties> map = applicationContext.getBeansOfType(MeterProperties.class);
//        map.forEach((beanName,mtkMeterProperties) -> mtkMeterConfiguration.addMeterProperty(mtkMeterProperties));
//        return mtkMeterConfiguration;
//    }

    @Bean
    @ConditionalOnMissingBean
    public MtkMeterRegistryConfiguration mtkMeterRegistryConfiguration(MtkMeterRegistryCommonProperties commonProperties){
        return new MtkMeterRegistryConfiguration(commonProperties);
    }

    @Bean
    @ConditionalOnBean({MtkMeterRegistryAutoConfiguration.class})
    @Autowired
    public MeterRegistryCustomizer<MeterRegistry> meterRegistryCustomizer(MtkMeterRegistryConfiguration mtkMeterRegistryConfiguration){
        return mtkMeterRegistryConfiguration::apply;
    }

    @Bean
    @Autowired
    public MetricTemplate metricTemplate(MeterRegistry meterRegistry){
        return new MetricTemplate(meterRegistry);
    }

    @Bean
    @Autowired
    @ConditionalOnMissingBean
    public TimedAspect timedAspect(MeterRegistry meterRegistry){
        return new TimedAspect();
    }
}
